package com.bj58.groupbuy.action.inter;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.bj58.groupbuy.action.model.AllianceBillingAttr;
import com.bj58.groupbuy.action.model.AllianceBillingMetrics;
import com.bj58.groupbuy.action.service.OnlineRevenueService;
import com.bj58.groupbuy.action.util.DateUtil;
import com.bj58.groupbuy.action.util.ExcelUtilNew;
import com.bj58.groupbuy.action.util.MathEval;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.security.Encrypt;

public class AllianceDataDeal {
	private OnlineRevenueService onlineRevenueService = ServiceLocator.get(OnlineRevenueService.class);
	private static HashMap<String,AllianceBillingAttr> allBillingMap = new HashMap<String, AllianceBillingAttr>();
	private List<AllianceBillingMetrics> speList = new ArrayList<AllianceBillingMetrics>();//特殊计算公式的集合
	
	public  String dealAllDatas(Map<String,List<String>> dealFileName) throws Exception{
		//获取所有有效计费名
		allBillingMap.clear();
		List<AllianceBillingAttr> allBillingList = onlineRevenueService.getAllianceBillingAttrList(" and state = 1 ", 20, -1).getItems();
		for(AllianceBillingAttr  vo : allBillingList){
			String key  = vo.getSource()  + ";" + vo.getAllianceName()+";"+vo.getBillingName()+";"+vo.getChannel();  // 渠道 + 联盟+计费名+渠道
			allBillingMap.put(key, vo);
		}
		String result = "";
		for(Iterator<String> it = dealFileName.keySet().iterator(); it.hasNext();){
			String key = it.next();
			
			String source = key.split("_")[0];
			String allianceName = key.split("_")[1];
			
			List<String> filePaths = dealFileName.get(key);
			try {
				boolean isSU = false;
				if("淘宝".equals(allianceName)){
					allianceName = "taobao";
					isSU = dealTaobaoData(filePaths,source);
				}else if("谷歌".equals(allianceName)){
					allianceName = "google";
					isSU = dealGoogleData(filePaths,source);
				}else if("百度".equals(allianceName)){
					allianceName = "baidu";
					isSU = dealBaiDuData(filePaths,source);
				}else{
					//表格各指标对应下标  依次存放的是 dateIndex/billName/showPv/clicks/amount
					List<Integer> indexsList = new ArrayList<Integer>();
					if("亿玛".equals(allianceName)){
						//亿玛 0 5 1 2 4
						indexsList.add(0);indexsList.add(5);indexsList.add(1);
						indexsList.add(2);indexsList.add(4);
					}else if("搜搜".equals(allianceName)){
						//搜搜 0 6 1 2 3
						indexsList.add(0);indexsList.add(6);indexsList.add(1);
						indexsList.add(2);indexsList.add(3);
					}else if("搜狗".equals(allianceName)){
						//搜狗 0 6 1 3 5
						indexsList.add(0);indexsList.add(6);indexsList.add(1);
						indexsList.add(3);indexsList.add(5);
					}else if("谷歌ADX广告".equals(allianceName)){
						//谷歌ADX 1 0 2 3 7
						indexsList.add(1);indexsList.add(0);indexsList.add(2);
						indexsList.add(3);indexsList.add(7);
					}else if("冉十科技".equals(allianceName)){
						//冉十 0 4 1 2 3
						indexsList.add(0);indexsList.add(4);indexsList.add(1);
						indexsList.add(2);indexsList.add(3);
					}else if("小猪".equals(allianceName)){
						//小猪 0 1 2 3 6
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(3);indexsList.add(6);
					}else if("360联盟".equals(allianceName)){
						//360 0 2 3 4 7
						indexsList.add(0);indexsList.add(2);indexsList.add(3);
						indexsList.add(4);indexsList.add(7);
					}else if("直客广告".equals(allianceName)){
						//直客 0 1 2 3 4
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(3);indexsList.add(4);
					}else if("品牌".equals(allianceName)){
						//品牌 0 1 2 3 4
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(3);indexsList.add(4);
					}else if("一起赢".equals(allianceName)){
						//一起赢 0 1 2 4 3
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(4);indexsList.add(3);
					}else if("易特".equals(allianceName)){
						//易特 0 1 2 4 3
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(4);indexsList.add(3);
					}else if("联告".equals(allianceName)){
						//联告 0 1 2 4 3
						indexsList.add(0);indexsList.add(1);indexsList.add(2);
						indexsList.add(4);indexsList.add(3);
					}else if("京东".equals(allianceName)){
						//京东 0 8 1 2 4
						indexsList.add(0);indexsList.add(8);indexsList.add(1);
						indexsList.add(2);indexsList.add(4);
					}
					//通用处理
					isSU = normalDeal(filePaths,source,allianceName,indexsList);
				}
				
				if(!isSU){
					result = result+" "+allianceName+" error; ";
				}
			} catch (Exception e) {
				e.printStackTrace();
				if("亿玛".equals(allianceName)){
					allianceName = "yima";
				}else if("搜搜".equals(allianceName)){
					allianceName = "sousou";
				}else if("搜狗".equals(allianceName)){
					allianceName = "sougou";
				}else if("谷歌ADX广告".equals(allianceName)){
					allianceName = "google ADX";
				}else if("冉十科技".equals(allianceName)){
					allianceName = "ranshi";
				}else if("小猪".equals(allianceName)){
					allianceName = "xiaozhu";
				}else if("360联盟".equals(allianceName)){
					allianceName = "360";
				}else if("直客广告".equals(allianceName)){
					allianceName = "zk";
				}else if("品牌".equals(allianceName)){
					allianceName = "pinpai";
				}else if("一起赢".equals(allianceName)){
					allianceName = "yiqiying";
				}else if("易特".equals(allianceName)){
					allianceName = "yite";
				}else if("联告".equals(allianceName)){
					allianceName = "liangao";
				}
				result = result+" "+allianceName+" error; ";
				continue;
			}
		
		}
		if("".equals(result)){
			result = "success";
		}
		return result;
	}
	
	/**通用数据处理
	 * @param filePaths  文件存放路径
	 * @param source	来源 58/驾校一点通
	 * @param allName   联盟名
	 * @param indexMap  表格中的指标字段下标对应关系，里面必须包含showPv/clicks/amount/billName/dateIndex
	 * @return
	 * @throws Exception
	 */
	private  boolean  normalDeal(List<String> filePaths ,String source,String allName, List<Integer> indexsList )throws Exception{
		//文件中指标的下标位置
		int dateIndex = indexsList.get(0); 
		int billName = indexsList.get(1); 
		int showPv = indexsList.get(2); 
		int clicks = indexsList.get(3); 
		int amount = indexsList.get(4); 
		
		for(String filePath : filePaths){
			File file = new File(filePath);
			List<String> deleteConditionList = new ArrayList<String>(); //待批量删除的数据
			if(file.isDirectory()){
				File[] fileArray = file.listFiles();   
				List<String> tempDataList = new ArrayList<String>();
				for(File fileName : fileArray){
					List<String>  ecList = ExcelUtilNew.readExcel(fileName.getPath());
					//去掉表头
					ecList.remove(0);
					tempDataList.addAll(ecList);
				}
				List<AllianceBillingMetrics>  abmList = new ArrayList<AllianceBillingMetrics>();
			
				AllianceBillingAttr aba = null;
				for(String temp : tempDataList){
					String[] tempArray = temp.split("@");
					if(tempArray[billName].contains("无代码")){ //谷歌ADX特殊处理
						continue;
					}
					String billingKey = source + ";" + allName + ";"+tempArray[billName]+";"; //从allBillingMap中获取已存储的计费名信息
					if(allBillingMap.containsKey(billingKey)){
						aba = allBillingMap.get(billingKey);
					}else{
						System.out.println("can not find :"+billingKey);
						continue;
					}
					AllianceBillingMetrics vo = new AllianceBillingMetrics();
					
					String date = tempArray[dateIndex];
					//360特殊处理
					if(date.contains("至")){
						date = date.substring(date.indexOf("至") + 1);
					}
					date = DateUtil.dealDateForBill(date);
//					String date = DateUtil.dealDateForBill(tempArray[dateIndex]);
					vo.setStatDate(date);
					vo.setAllianceName(aba.getAllianceName());
					vo.setAdvertType(aba.getAdvertType());
					vo.setBillingName(aba.getBillingName());
					vo.setChannel(aba.getChannel());
					vo.setCate1(aba.getCate1());
					vo.setCate2(aba.getCate2());
					vo.setShowPv(Math.round(Double.valueOf(tempArray[showPv].trim().replace(",", ""))));
					vo.setClicks(Math.round(Double.valueOf(tempArray[clicks].trim().replace(",", ""))));
					vo.setAmount(Double.valueOf(tempArray[amount].trim().replace(",", "")));
					vo.setAdvertLocation(aba.getAdvertLocation());
					//收入计算
					String formula = aba.getFormula();
					if(!"".equals(formula) && null != formula){
						formula = formula.replace("clicks", String.valueOf(vo.getClicks()));
						formula = formula.replace("showPv", String.valueOf(vo.getShowPv()));
						formula = formula.replace("income", String.valueOf(vo.getAmount()));
						double famount = MathEval.eval(formula);
						vo.setAmount(famount);
					}
					//需删除数据的MD5
					String deleteMD5 = source + allName + date + tempArray[billName];
					deleteMD5 = Encrypt.MD532(deleteMD5);
					deleteConditionList.add(deleteMD5);
					vo.setDeleteMd5(deleteMD5);
					
					abmList.add(vo);
				}
				//批量删除数据
				StringBuffer  deleConditions = new StringBuffer();
				deleConditions.append(" and deleteMd5 in(");
				for(int i= 0 ; i< deleteConditionList.size() ; i++ ){
					String  md5 =  deleteConditionList.get(i);
					deleConditions.append("'"+md5+"'");
					if( (i+1) < deleteConditionList.size() ){
						deleConditions.append(",");
					}else {
						deleConditions.append(")");
					}
				}
				if(!onlineRevenueService.deleteABMList(deleConditions.toString())){
					return false;
				}
				//插入新数据
				if(!onlineRevenueService.inserABMList(abmList,source)){
					return false;
				}
			}
		}
		return true;
	}
	
	
	/**淘宝数据处理
	 * @param filePath
	 * @return
	 */
	private  boolean  dealTaobaoData(List<String> filePaths ,String source)throws Exception{
		//文件中指标的下标位置
		int dateIndex = 0;//日期
		int billName = 2;//计费名
		
		int showPv_TANX = 5;  //联盟 展现量
		int clicks_TANX = 6; //TANX点击量
		int income_TANX = 9;  //收入
		
		
		int clicks_ztc = 8; //直通车 点击量
		int income_ztc = 9; //直通车 收入
		
		for(String filePath : filePaths){
			File file = new File(filePath);
			if(file.isDirectory()){
				//读取文件夹下所有文件数据  汇总
				File[] fileArray = file.listFiles();   
				List<String> tempDataList = new ArrayList<String>();
				for(File fileName : fileArray){
					List<String>  ecList = ExcelUtilNew.readExcel(fileName.getPath());
					//去掉表头
					ecList.remove(0);
					tempDataList.addAll(ecList);
				}
				
				//计算匹配
				List<AllianceBillingMetrics>  abmList = new ArrayList<AllianceBillingMetrics>();
				List<String>  deleteConditionList = new ArrayList<String>();//需删除数据;
				for(String temp : tempDataList){
					String[] tempArray = temp.split("@");
					//直通车和联盟渠道
					List<String>  channelList = new ArrayList<String>();
//					channelList.add("直通车");
					channelList.add("TANX");
					AllianceBillingAttr aba = null;
					for(String channel : channelList){
						String billingKey = source + ";" + "淘宝;"+tempArray[billName]+";"+channel; //从allBillingMap中获取已存储的计费名信息
						if(allBillingMap.containsKey(billingKey)){
							aba = allBillingMap.get(billingKey);
						}else{
							System.out.println("can not find :"+billingKey);
							continue;
						}
						String date = DateUtil.dealDateForBill(tempArray[dateIndex]); 
						AllianceBillingMetrics vo = new AllianceBillingMetrics();
						vo.setStatDate(date);
						vo.setAllianceName(aba.getAllianceName());
						vo.setAdvertType(aba.getAdvertType());
						vo.setBillingName(aba.getBillingName());
						vo.setChannel(aba.getChannel());
						vo.setCate1(aba.getCate1());
						vo.setCate2(aba.getCate2());
						vo.setAdvertLocation(aba.getAdvertLocation());
						if("直通车".equals(channel)){
							vo.setClicks((long) Double.parseDouble(tempArray[clicks_ztc].replaceAll(",", "")));
							vo.setAmount(Double.valueOf(tempArray[income_ztc].replaceAll(",", "")));
						}else if("TANX".equals(channel)){
							vo.setShowPv((long) Double.parseDouble(tempArray[showPv_TANX].replaceAll(",", "")));
							vo.setClicks((long) Double.parseDouble(tempArray[clicks_TANX].replaceAll(",", "")));
							vo.setAmount(Double.valueOf(tempArray[income_TANX].replaceAll(",", "")));
						}
						//需删除数据的MD5
						String deleteMD5 = source +  "淘宝"+date+tempArray[billName]+channel;
						deleteMD5 = Encrypt.MD532(deleteMD5);
						deleteConditionList.add(deleteMD5);
						vo.setDeleteMd5(deleteMD5);
						
						abmList.add(vo);
					}
				}
				//删除旧数据
				StringBuffer  deleConditions = new StringBuffer();
				deleConditions.append(" and deleteMd5 in(");
				for(int i= 0 ; i< deleteConditionList.size() ; i++ ){
					String  md5 =  deleteConditionList.get(i);
					deleConditions.append("'"+md5+"'");
					if( (i+1) < deleteConditionList.size() ){
						deleConditions.append(",");
					}else {
						deleConditions.append(")");
					}
				}
				if(!onlineRevenueService.deleteABMList(deleConditions.toString())){
					return false;
				}
				//插入新数据
				if(!onlineRevenueService.inserABMList(abmList,source)){
					return false;
				}
			}
		}
		return true;
	}
	
	
	/**谷歌数据处理
	 * @param filePath
	 * @return
	 */
	private  boolean  dealGoogleData(List<String> filePaths ,String source)throws Exception{
		
		List<String> yaoList = new ArrayList<String>();//老姚账户数据
		List<String> duankeList = new ArrayList<String>();//段柯账户数据
		List<String>  deleteMDFiveList = new ArrayList<String>();//需删除的数据的MD5
		List<AllianceBillingMetrics>  abmList = new ArrayList<AllianceBillingMetrics>(); //待插入的数据
		
		for(String filePath : filePaths){
			File file = new File(filePath);
			if(file.isDirectory()){
				File[] fileArray = file.listFiles();   
				for(File fileName : fileArray){
					List<String>  ecList = new ArrayList<String>();
					if(fileName.getPath().contains("csv")){
						ecList = ExcelUtilNew.readCsv(fileName.getPath(), "unicode");
					}else{
						ecList = ExcelUtilNew.readExcel(fileName.getPath());
					}
					//去掉表头
					ecList.remove(0);
					//GOOGLE文件分3类   全部数据   两个账户的数据    全部数据匹配计费名处理    其他两类数据放入总计数据集合  待处理
					if(fileName.getName().contains("google_all")){ //渠道数据
						String date = getDateFromPath(fileName.getName());
						//文件中指标的下标位置
						int clicks = 4;  
						int showPv = 2;
						int income = 8;
						int billNameIndex = 1;
						int channelIndex = 0;
						
						AllianceBillingAttr aba = null;
						for(String temp : ecList){
							String[] tempArray = temp.split("@");
							String billingKey = source + ";" + "谷歌;"+tempArray[billNameIndex]+";"+tempArray[channelIndex]; //从allBillingMap中获取已存储的计费名信息
							if(allBillingMap.containsKey(billingKey)){
								aba = allBillingMap.get(billingKey);
							}else{
								System.out.println("can not find :"+billingKey);
								continue;
							}
							AllianceBillingMetrics vo = new AllianceBillingMetrics();
							vo.setStatDate(date);
							vo.setAllianceName(aba.getAllianceName());
							vo.setAdvertType(aba.getAdvertType());
							vo.setBillingName(aba.getBillingName());
							vo.setChannel(aba.getChannel());
							vo.setCate1(aba.getCate1());
							vo.setCate2(aba.getCate2());
							vo.setAdvertLocation(aba.getAdvertLocation());
							if("".equals(tempArray[clicks].trim())){
								vo.setClicks(0);
							}else{
								vo.setClicks(Math.round(Double.valueOf(tempArray[clicks].trim().replace(",", ""))));
							}
							
							if("".equals(tempArray[showPv].trim())){
								vo.setShowPv(0);
							}else{
								vo.setShowPv(Math.round(Double.valueOf(tempArray[showPv].trim().replace(",", ""))));
							}
							
							if("".equals(tempArray[income])){
								vo.setAmount(0);
							}else{
								vo.setAmount(Double.valueOf(tempArray[income].replace(",", "")));
							}
							//收入计算
							String formula = aba.getFormula();
							if(!"".equals(formula) && null != formula){
								formula = formula.replace("showPv", String.valueOf(vo.getShowPv()));
								formula = formula.replace("clicks", String.valueOf(vo.getClicks()));
								formula = formula.replace("income", String.valueOf(vo.getAmount()));
								double amount = MathEval.eval(formula);
								vo.setAmount(amount);
							}
							//需删除数据的MD5
							String deleteMD5 =  source + "谷歌"+date+tempArray[billNameIndex]+tempArray[channelIndex];
							deleteMD5 = Encrypt.MD532(deleteMD5);
							deleteMDFiveList.add(deleteMD5);
							//待插入数据
							vo.setDeleteMd5(deleteMD5);
							abmList.add(vo);
						}
					}else if(fileName.getName().contains("yaojb")){ //老姚
						yaoList.addAll(ecList);
					}else if(fileName.getName().contains("duanke")){ //段柯
						duankeList.addAll(ecList);
					}else if(fileName.getName().contains("customer")){ //会员数据
						//文件中指标的下标位置
						int clicks = 4;  
						int showPv = 2;
						int income = 8;
						int billNameIndex = 0;
						int dateIndex = 1;
						
						AllianceBillingAttr aba = null;
						for(String temp : ecList){
							String[] tempArray = temp.split("@");
							String billingKey = source + ";" + "谷歌;"+tempArray[billNameIndex]+";总计"; //从allBillingMap中获取已存储的计费名信息
							if(allBillingMap.containsKey(billingKey)){
								aba = allBillingMap.get(billingKey);
							}else{
								System.out.println("can not find :"+billingKey);
								continue;
							}
							String date = DateUtil.dealDateForBill(tempArray[dateIndex]);
							
							AllianceBillingMetrics vo = new AllianceBillingMetrics();
							vo.setStatDate(date);
							vo.setAllianceName(aba.getAllianceName());
							vo.setAdvertType(aba.getAdvertType());
							vo.setBillingName(aba.getBillingName());
							vo.setChannel(aba.getChannel());
							vo.setCate1(aba.getCate1());
							vo.setCate2(aba.getCate2());
							vo.setAdvertLocation(aba.getAdvertLocation());
							if("".equals(tempArray[clicks].trim())){
								vo.setClicks(0);
							}else{
								vo.setClicks(Math.round(Double.valueOf(tempArray[clicks].trim().replace(",", ""))));
								
							}
							
							if("".equals(tempArray[showPv].trim())){
								vo.setShowPv(0);
							}else{
								vo.setShowPv(Math.round(Double.valueOf(tempArray[showPv].trim().replace(",", ""))));
							}
							
							if("".equals(tempArray[income])){
								vo.setAmount(0);
							}else{
								vo.setAmount(Double.valueOf(tempArray[income].replace(",", "")));
							}
							//收入计算
							String formula = aba.getFormula();
							if(!"".equals(formula) && null != formula){
								formula = formula.replace("showPv", String.valueOf(vo.getShowPv()));
								formula = formula.replace("clicks", String.valueOf(vo.getClicks()));
								formula = formula.replace("income", String.valueOf(vo.getAmount()));
								double amount = MathEval.eval(formula);
								vo.setAmount(amount);
							}
							//需删除数据的MD5
							String deleteMD5 =  source + "谷歌"+date+tempArray[billNameIndex]+"总计";
							deleteMD5 = Encrypt.MD532(deleteMD5);
							deleteMDFiveList.add(deleteMD5);
							//待插入数据
							vo.setDeleteMd5(deleteMD5);
							abmList.add(vo);
						}
					}
				}
			}
		}
		//处理总计数据 ：老姚+段柯账户数据 
		//1. google afc = duanke_内容广告 + yao_all
		//2. google afs = duanke_搜索广告
		//取出谷歌AFS总计计费对象 
		AllianceBillingAttr afsAll = allBillingMap.get(source + ";谷歌;总计;AFS");
		//取出谷歌AFC总计计费对象 
		AllianceBillingAttr afcAll = allBillingMap.get(source + ";谷歌;总计;AFC");
		
		Map<String,AllianceBillingMetrics> afcMap = new 	HashMap<String, AllianceBillingMetrics>();//KEY = 日期
		//段柯账户
		int clicks_dk = 4;  
		int showPv_dk = 2;
		int income_dk = 8;
		int advertType_dk = 0;
		int date_dk = 1;
		for(String values : duankeList){
			String[] valuesArray = values.split("@");
			String type = valuesArray[advertType_dk]; //广告类型
			
			AllianceBillingMetrics  vo = new AllianceBillingMetrics();
			if(type.contains("内容")){ //afc
				vo.setAllianceName(afcAll.getAllianceName());
				vo.setAdvertType(afcAll.getAdvertType());
				vo.setBillingName(afcAll.getBillingName());
				vo.setChannel(afcAll.getChannel());
			}else if(type.contains("搜索")){ //afs
				vo.setAllianceName(afsAll.getAllianceName());
				vo.setAdvertType(afsAll.getAdvertType());
				vo.setBillingName(afsAll.getBillingName());
				vo.setChannel(afsAll.getChannel());
			}
			
			
			String statDate = DateUtil.dealDateForBill(valuesArray[date_dk]);
			vo.setStatDate(statDate);
			
			//兼容 一点通  改为累加
			if(afcMap.containsKey(statDate)){
				vo = afcMap.get(statDate);
			}
			
			
			if(!"".equals(valuesArray[showPv_dk])){//showPv
				vo.setShowPv(Math.round(Double.valueOf(valuesArray[showPv_dk].replace(",", "")) + vo.getShowPv()));
			}
			if(!"".equals(valuesArray[clicks_dk])){//点击
				vo.setClicks(Math.round(Double.valueOf(valuesArray[clicks_dk].replace(",", "").toString())) + vo.getClicks());
			}
			if(!"".equals(valuesArray[income_dk])){//收入
				if("58".equals(source)){
					if(type.contains("内容")){
						vo.setAmount(Double.parseDouble(valuesArray[income_dk].replace(",", ""))*6.17*0.56);
					}else if(type.contains("搜索")){
						vo.setAmount(Double.parseDouble(valuesArray[income_dk].replace(",", ""))*6.17*0.54);
					}
				}else{
					if(type.contains("内容")){
						vo.setAmount(Double.parseDouble(valuesArray[income_dk].replace(",", ""))*6.17 + vo.getAmount());
					}else if(type.contains("搜索")){
						vo.setAmount(Double.parseDouble(valuesArray[income_dk].replace(",", ""))*6.17 + vo.getAmount());
					}
				}
			
			}
			
			String deleteMD5 =  source + "谷歌"+vo.getStatDate()+vo.getBillingName()+vo.getChannel();
			deleteMD5 = Encrypt.MD532(deleteMD5);
			deleteMDFiveList.add(deleteMD5);
			vo.setDeleteMd5(deleteMD5);
			if(type.contains("内容")){ //afc
				afcMap.put(vo.getStatDate(), vo);
			}else if(type.contains("搜索")){ //afs
				abmList.add(vo);
			}
		}
		//老姚账户  与段柯账户中的AFC数据累加计算
		int clicks_yjb = 2;  
		int showPv_yjb = 1;
		int income_yjb = 6;
		int date_yjb = 0;
		for(String values : yaoList){
			String[] valuesArray = values.split("@");
			String date = DateUtil.dealDateForBill(valuesArray[date_yjb]);
			AllianceBillingMetrics  vo = new AllianceBillingMetrics();
			if(afcMap.containsKey(date)){
				vo = afcMap.get(date);
				afcMap.remove(date);
			}else{
				vo.setAllianceName(afcAll.getAllianceName());
				vo.setAdvertType(afcAll.getAdvertType());
				vo.setBillingName(afcAll.getBillingName());
				vo.setChannel(afcAll.getChannel());
				vo.setStatDate(date);
			}
			
			if(!"".equals(valuesArray[showPv_yjb])){//showPv
				vo.setShowPv(vo.getShowPv()+Math.round(Double.valueOf(valuesArray[showPv_yjb].replace(",", ""))));
			}
			
			if(!"".equals(valuesArray[clicks_yjb])){//点击
				vo.setClicks(vo.getClicks()+Math.round(Double.valueOf(valuesArray[clicks_yjb].replace(",", ""))));
			}
			
			if(!"".equals(valuesArray[income_yjb])){//收入
				vo.setAmount(vo.getAmount()+Double.parseDouble(valuesArray[income_yjb].replace(",", "")));
			}
			abmList.add(vo);
		}
		
		
		//驾校一点通兼容处理  老姚账户为0
		if(afcMap.size() > 0 ){
			for(Iterator<String> it = afcMap.keySet().iterator(); it.hasNext();){
				String key = it.next();
				abmList.add(afcMap.get(key));
			}
		}
	
	
		//批量删除数据
		StringBuffer  deleConditions = new StringBuffer();
		deleConditions.append(" and deleteMd5 in(");
		for(int i= 0 ; i< deleteMDFiveList.size() ; i++ ){
			String  md5 =  deleteMDFiveList.get(i);
			deleConditions.append("'"+md5+"'");
			if( (i+1) < deleteMDFiveList.size() ){
				deleConditions.append(",");
			}else {
				deleConditions.append(")");
			}
		}
		if(!onlineRevenueService.deleteABMList(deleConditions.toString())){
			return false;
		}
		//批量插入数据
		if(!onlineRevenueService.inserABMList(abmList,source)){
			return false;
		}
		
		return true;
	}
	
	
	/**百度基础数据处理
	 * @param filePaths
	 * @return
	 * @throws Exception
	 */
	private  boolean  dealBaiDuData(List<String> filePaths ,String source)throws Exception{
		//初始下标
		int showPv = 0;  
		int clicks = 0;
		int amount = 0;
		final int  dateIndex = 0;//固定
		int channelIndex = 0;
		
		for(String filePath : filePaths){
			File file = new File(filePath);
			if(file.isDirectory()){
				File[] fileArray = file.listFiles();   
				List<AllianceBillingMetrics>  abmList = new ArrayList<AllianceBillingMetrics>();  //待插入得数据
				List<String>  deleteMDFiveList = new ArrayList<String>();//需删除的数据MD5集合
				//迭代依次处理各个文件
				for(File subFile : fileArray){
					if(subFile.isDirectory()){
						continue;
					}
					List<String>  ecList = ExcelUtilNew.readExcel(subFile.getPath());
					//根据文件名获取计费名 并且根据文件第一列标题是否含有渠道信息来区分非总计数据是分渠道的还是不分，确认各指标的下标
					String billName  = "";
					if(subFile.getName().contains("ss")){ //搜索
						if(subFile.getName().contains("ss_all")){
							billName = "ss_all"; //搜索总计数据
							showPv = 1;
							clicks = 2;
							amount = 4;
							channelIndex = -1;//无渠道则标为-1
						}else{
							if(subFile.getName().contains("jfm")){ //各计费名总计数据表
								showPv = 2;
								clicks = 3;
								amount = 5;
								channelIndex = -1;
							}else{//计费名渠道数据表
								billName = subFile.getName().replace("baidu_ss_", "");
								billName = billName.substring(0,billName.lastIndexOf("."));
								showPv = 2;
								clicks = 3;
								amount = 5;
								channelIndex = 1;
							}
						}
					}else if(subFile.getName().contains("wm")){//网盟
						if(subFile.getName().contains("wm_all")){
							billName = "wm_all"; //网盟总计数据
							showPv = 2;
							clicks = 3;
							amount = 5;
							channelIndex = -1;
						}else{
							if(subFile.getName().contains("jfm")){ //各计费名总计数据表
								showPv = 3;
								clicks = 4;
								amount = 6;
								channelIndex = -1;
							}else{//计费名渠道数据表
								billName = subFile.getName().replace("baidu_wm_", "");
								billName = billName.substring(0,billName.lastIndexOf("."));
								showPv = 3;
								clicks = 5;
								amount = 7;
								channelIndex = 2;
							}
						}
					}else if(subFile.getName().contains("app")){//APP
						if(subFile.getName().contains("app_all")){//APP总计数据
							billName = "app_all"; //网盟总计数据
							showPv = 1;
							clicks = 2;
							amount = 5;
							channelIndex = -1;
						}else{//APP渠道  预留占位
							//TODO
						}
					}
					
					ecList.remove(0);	//去掉表头
					AllianceBillingAttr aba = null;
					// 逐行迭代匹配数据
					
					System.out.println(subFile.getName());
					for(String temp : ecList){
						String[] tempArray = temp.split("@");
						
						//all里面的jfm文件 billName需轮询读
						if(subFile.getName().contains("jfm")){
							billName = tempArray[1];
						}
						
						String billingKey = source + ";" + "百度;"+billName+";"; //从allBillingMap中获取已存储的计费名信息
						if(channelIndex > -1){ //含渠道的数据  忽略全部渠道/全部代码位的数据  其他的在billingKey后加上渠道名
							if(tempArray[channelIndex].contains("全部")){
								continue;
							}
							billingKey = billingKey+tempArray[channelIndex];
						}else{
							billingKey = billingKey+"总计";
						}
						
						if(allBillingMap.containsKey(billingKey)){
							aba = allBillingMap.get(billingKey);
						}else{
							System.out.println("can not find :"+billingKey);
							continue;
						}
						String date = DateUtil.dealDateForBill(tempArray[dateIndex]).replace("\"", "");
						AllianceBillingMetrics vo = new AllianceBillingMetrics();
						vo.setStatDate(date);
						vo.setAllianceName(aba.getAllianceName());
						vo.setAdvertType(aba.getAdvertType());
						vo.setBillingName(aba.getBillingName());
						vo.setChannel(aba.getChannel());
						vo.setCate1(aba.getCate1());
						vo.setCate2(aba.getCate2());
						vo.setShowPv(Math.round(Double.valueOf(tempArray[showPv].trim().replace(",", "").replace("\"", ""))));
						vo.setClicks(Math.round(Double.valueOf(tempArray[clicks].trim().replace(",", "").replace("\"", ""))));
						vo.setAmount(Double.valueOf(tempArray[amount].trim().replace(",", "").replace("\"", "")));
						vo.setAdvertLocation(aba.getAdvertLocation());
						//收入计算
						String formula = aba.getFormula();
						if(!"".equals(formula) && null != formula){
							formula = formula.replace("clicks", String.valueOf(vo.getClicks()));
							formula = formula.replace("showPv", String.valueOf(vo.getShowPv()));
							formula = formula.replace("income", String.valueOf(vo.getAmount()));
							
							//替换完之后 还有字母的  作为特殊计算公式处理
							if(checkSpeFormula(formula)){
								speList.add(vo);
								continue;
							}
							
							double famount = MathEval.eval(formula);
							vo.setAmount(famount);
						}
						
						String deleteMD5 =  source + "百度"+vo.getStatDate()+vo.getBillingName()+vo.getChannel();//删除用的MD5
						deleteMD5 = Encrypt.MD532(deleteMD5);
						deleteMDFiveList.add(deleteMD5);
						vo.setDeleteMd5(deleteMD5);
						//填入待插入数据集合
						abmList.add(vo);
					}
				}
				//批量删除数据
				StringBuffer  deleConditions = new StringBuffer();
				deleConditions.append(" and deleteMd5 in(");
				for(int i= 0 ; i< deleteMDFiveList.size() ; i++ ){
					String  md5 =  deleteMDFiveList.get(i);
					deleConditions.append("'"+md5+"'");
					if( (i+1) < deleteMDFiveList.size() ){
						deleConditions.append(",");
					}else {
						deleConditions.append(")");
					}
				}
				if(!deleteMDFiveList.isEmpty() && deleteMDFiveList != null){
					if(!onlineRevenueService.deleteABMList(deleConditions.toString())){
						return false;
					}
				}
				//插入新数据
				if(!onlineRevenueService.inserABMList(abmList,source)){
					return false;
				}
			}
		}
		return true;
	}
	
	/**正则从文件夹路径中获取日期
	 * @param path
	 * @return
	 */
	private  String getDateFromPath(String path){
        Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})"); 
        Matcher m = p.matcher(path);
        while (m.find()) {
            if (!"".equals(m.group()))
              return m.group();
        }
        return null;
	}
	
	/**检查计算公式是否包含字母
	 * @param formula
	 * @return
	 */
	private boolean checkSpeFormula(String formula){
		 boolean hasLetter = false;//定义一个boolean值，用来表示是否包含字母
         for(int i=0 ; i<formula.length() ; i++){ //循环遍历字符串   
             if(Character.isLetter(formula.charAt(i))){   //用char包装类中的判断字母的方法判断每一个字符
            	 hasLetter = true;
             }
         }
        return hasLetter;
	}
	
	public static void main(String[] args) {
		String a = "458230.00";
		System.out.println(Math.round(Double.valueOf(a)));
	}
}
